package weka;


import weka.associations.Associator;
import weka.core.Instances;
import weka.core.OptionHandler;
import weka.core.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by taohj on 2014/12/30.
 */
public class AssociationsHelpper extends  AbstractHelpper{


    public AssociationsHelpper(String location, String ClassStr,String options) throws Exception {
        super(location, ClassStr,options);
    }




    public static void main(String[] args) throws Exception {



        System.out.println(new AssociationsHelpper("C:\\Users\\taohj\\Desktop\\data\\supermarket.arff", "weka.associations.Apriori","").start());
    }

    @Override
    public StringBuffer start() throws Exception {

        Instances inst = new Instances(this.getM_Instances());
        String grph = null;


        // String xmlRules = null;

        Associator associator = (Associator)Class.forName(this.getClassName()).newInstance();
        if (associator instanceof  OptionHandler){
            ((OptionHandler) associator).setOptions(Utils.splitOptions(this.getOption()));
            System.out.println("-----setOption:"+this.getOption()+"-----");
        }
        StringBuffer outBuff = new StringBuffer();
        String name = (new SimpleDateFormat("HH:mm:ss - "))
                .format(new Date());
        String cname = associator.getClass().getName();
        if (cname.startsWith("weka.associations.")) {
            name += cname.substring("weka.associations.".length());
        } else {
            name += cname;
        }
        String cmd = associator.getClass().getName();
        if (associator instanceof OptionHandler) {
            cmd += " "
                    + Utils.joinOptions(((OptionHandler)associator).getOptions());
        }

        outBuff.append("=== Run information ===\n\n");
        outBuff.append("Scheme:       " + cname);
        if (associator instanceof OptionHandler) {
            String[] o = ((OptionHandler) associator).getOptions();
            outBuff.append(" " + Utils.joinOptions(o));
        }
        outBuff.append("\n");
        outBuff.append("Relation:     " + inst.relationName() + '\n');
        outBuff.append("Instances:    " + inst.numInstances() + '\n');
        outBuff.append("Attributes:   " + inst.numAttributes() + '\n');
        if (inst.numAttributes() < 100) {
            for (int i = 0; i < inst.numAttributes(); i++) {
                outBuff.append("              " + inst.attribute(i).name()
                        + '\n');
            }
        } else {
            outBuff.append("              [list of attributes omitted]\n");
        }

        associator.buildAssociations(inst);
        outBuff.append("=== Associator model (full training set) ===\n\n");
        outBuff.append(associator.toString() + '\n');
        return outBuff;
    }
}
